/**
 *  @file JSONHandler.h
 */
#ifndef _JSONHANDLER_H_
#define _JSONHANDLER_H_

#include "../common/BaseObject.h"
#include "../text/CFString.h"
#include "../collections/LinkedHashtableT.h"

namespace cppflib
{

namespace json
{

/** 
 *  Handler for derived class to implement so as to receive notifications from a JSONQuickParser
 *  when parsing a JSON stream
 */
class _DLLAPI JSONHandler : public BaseObject
{
public:
    typedef enum
    {
       HERR_WARN = 0x01,
       HERR_ERROR = 0x02,
       HERR_FATAL = 0x04
    } jsonhandlererror_t;

private:
    int objNestedLevel;   ///< root object is at level 0
    int arrayNestedLevel; ///< first array is at level 0
    CFString currentNVPairName;   
    int handlerError;

protected:
    JSONHandler(void)
    {
       this->objNestedLevel = -1;
       this->arrayNestedLevel = -1;
       this->handlerError = 0;
    }

    int GetObjNestedLevel() { return objNestedLevel; }
    int GetArrayNestedLevel() { return arrayNestedLevel; }
    const CFString & GetCurNVPairName() { return currentNVPairName; }

public:
    
    virtual ~JSONHandler(void)
    {
    }

    // these are called by parser
    void IncrementObjectNestedLevel() { ++this->objNestedLevel; }
    void DecrementObjectNestedLevel() { --this->objNestedLevel; }
    void IncrementArrayNestedLevel() { ++this->arrayNestedLevel; }
    void DecrementArrayNestedLevel() { --this->arrayNestedLevel; }
    void SetCurNVPairName(const CFString &curName) { this->currentNVPairName = curName; }
    void ClearErrorWarning() { this->handlerError = 0; }
    void MarkErrorWarning(jsonhandlererror_t err) { this->handlerError |= static_cast<int>(err); }
    bool IsErrorMarked() { return ((this->handlerError & HERR_ERROR) != 0 || 
                                   (this->handlerError & HERR_FATAL) != 0); }

    // below should be overriden by derived class

    virtual void StartObject() = 0;
    virtual void EndObject() = 0;
    virtual void StartNVPair(const CFString & nvPairName) = 0;
    virtual void ValueString(const CFString & v) = 0;
    virtual void ValueInt(const int v) = 0;
    virtual void ValueInt64(const i64_t v) = 0;
    virtual void ValueDouble(const double v) = 0;
    virtual void ValueBoolean(const bool v) = 0;
    virtual void ValueNull() = 0;
    virtual void EndNVPair() = 0;
    virtual void StartArray() = 0;
    virtual void EndArray() = 0;

    virtual void Error(const CFString &msg) = 0;
    virtual void FatalError(const CFString &msg) = 0;
    virtual void Warning(const CFString &msg) = 0;
};

} // end of namespace json

} // end of namespace cppflib


#endif
